home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / devel / gencodec / source / tools.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  22KB  |  827 lines

  1. #include <proto/exec.h>
  2. #include <proto/dos.h>
  3. #include <proto/intuition.h>
  4. #include <exec/memory.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7.  
  8. #include "Tools.h"
  9.  
  10. struct MemNode
  11. {
  12.     struct MemNode *child;        /* Next block in list        */
  13.  
  14.     char    *File;                /* File that allocated us    */
  15.     ULONG    Line;                /* Line we were allocated on */
  16.  
  17.     ULONG    size;                /* Size of memory block      */
  18. };
  19.  
  20. struct FileNode
  21. {
  22.     struct FileNode *child;        /* Next block in list        */
  23.  
  24.     char    *File;                /* File that opened file     */
  25.     ULONG    Line;                /* Line we were opened file  */
  26.  
  27.     BOOL    amigafile;            /* It is an amiga file       */
  28.     char    *filename;            /* The filename of the file  */
  29.  
  30.     union
  31.     {
  32.         BPTR    afile;            /* amiga descriptor file     */
  33.         FILE    *cfile;            /* c descriptor file         */
  34.     } file_desc;
  35. };
  36.  
  37. static struct MemNode    *memhead  = NULL;
  38. static ULONG             Total_RAM = 0;
  39. static ULONG             Max_RAM = 0;
  40. static struct FileNode    *filehead = NULL;
  41.  
  42. static void             *Data_f = NULL;
  43. static TypeQuitFunction Quit_f = NULL;    /* Function called if an error occured    */
  44.                                     /* cf SetFunctionQuit and safe_Quit        */
  45.  
  46. /****************************************************************************************************************/
  47. /****                                                                                                         ****/
  48. /**                                            DisplayMsg                                                           **/
  49. /****                                                                                                         ****/
  50. /****************************************************************************************************************/
  51.  
  52. void DisplayMsg(char *Msg)
  53. {
  54.     struct EasyStruct RequestMsg =
  55.     {
  56.         sizeof(struct EasyStruct),
  57.         0,
  58.         "GenCode C Error",
  59.         "%s",
  60.         "OK"
  61.     };
  62.  
  63.     EasyRequest(NULL,&RequestMsg,NULL,(long)Msg);
  64. }
  65.  
  66. /****************************************************************************************************************/
  67. /****                                                                                                         ****/
  68. /**                                            safe_Quit                                                           **/
  69. /****                                                                                                         ****/
  70. /****************************************************************************************************************/
  71.  
  72. static void safe_Quit(void)
  73. {
  74.     if (Quit_f)
  75.     {
  76.         (*Quit_f)(Data_f);
  77.     }
  78.     else
  79.     {
  80.         DisplayMsg("Oups!!! There is no Quit function.\nReport this bug to the authors");
  81.         /* Try to quit properly */
  82.         CloseAllFiles(TRUE);
  83.         ClearMemory(TRUE);
  84.         exit(20);
  85.     }
  86. }
  87.  
  88. /****************************************************************************************************************/
  89. /****                                                                                                         ****/
  90. /**                                            SetFunctionQuit                                                       **/
  91. /****                                                                                                         ****/
  92. /****************************************************************************************************************/
  93.  
  94. TypeQuitFunction SetFunctionQuit(TypeQuitFunction Quit_function)
  95. {
  96.     TypeQuitFunction    quit_f = Quit_f;
  97.  
  98.     Quit_f = Quit_function;
  99.  
  100.     return quit_f;
  101. }
  102.  
  103. /****************************************************************************************************************/
  104. /****                                                                                                         ****/
  105. /**                                            SetDataQuit                                                           **/
  106. /****                                                                                                         ****/
  107. /****************************************************************************************************************/
  108.  
  109. void *SetDataQuit(void *Data)
  110. {
  111.     void    *data_f = Data_f;
  112.  
  113.     Data_f = Data;
  114.  
  115.     return data_f;
  116. }
  117.  
  118. /****************************************************************************************************************/
  119. /****                                                                                                         ****/
  120. /**                                            safe_AllocMemory                                                   **/
  121. /****                                                                                                         ****/
  122. /****************************************************************************************************************/
  123.  
  124. void *safe_AllocMemory(ULONG byteSize,BOOL quit,char *File,ULONG Line)
  125. {
  126.     static char msg[150];
  127.     struct MemNode *t;
  128.  
  129.     if (memhead)
  130.         t = memhead;
  131.     else
  132.         t = NULL;
  133.  
  134.     if (!(memhead = AllocMem(byteSize + sizeof(struct MemNode),MEMF_CLEAR)))
  135.     {
  136.         sprintf(msg,"ERROR !!!!! NOT ENOUGH MEMORY !!!\nCan't allocate %ld bytes\nFile %s Line %ld",byteSize,File,Line);
  137.         DisplayMsg(msg);
  138.         memhead = t;
  139.         if (!quit)
  140.             return NULL;
  141.         else
  142.             safe_Quit();
  143.     }
  144.  
  145.     memhead->child = t;
  146.     memhead->File = File;
  147.     memhead->Line = Line;
  148.     memhead->size = byteSize;
  149.     Total_RAM += byteSize+sizeof(struct MemNode);
  150.     Max_RAM = (Max_RAM>Total_RAM) ? Max_RAM : Total_RAM;
  151.  
  152.     return ((void *)((ULONG)memhead + sizeof(struct MemNode)));
  153. }
  154.  
  155. /****************************************************************************************************************/
  156. /****                                                                                                         ****/
  157. /**                                            safe_FreeMemory                                                       **/
  158. /****                                                                                                         ****/
  159. /****************************************************************************************************************/
  160.  
  161. void safe_FreeMemory(void *ptr,char* File,ULONG Line)
  162. {
  163.     static char msg[100];
  164.     struct MemNode *t1,*t2;
  165.  
  166.     if (ptr == NULL)
  167.         return;
  168.  
  169.     t1 = memhead;
  170.     t2 = NULL;
  171.  
  172.     while(t1)
  173.     {
  174.         if((ULONG)ptr == (ULONG)t1 + sizeof(struct MemNode))
  175.         {
  176.             if(t2)
  177.                 t2->child = t1->child; /* Remove block from the list */
  178.             else
  179.                 memhead = t1->child;
  180.  
  181.             Total_RAM -= t1->size+sizeof(struct MemNode);
  182.             FreeMem(t1,t1->size+sizeof(struct MemNode));
  183.             return;
  184.         }
  185.         t2 = t1;
  186.         t1 = t1->child;
  187.       }
  188.  
  189.     sprintf(msg,"%s %ld: Free twice (?) FreeMem($%lx)",File,Line,(ULONG)ptr - sizeof(struct MemNode));
  190.     DisplayMsg(msg);
  191.     safe_ShowMemory(File,Line);
  192. }
  193.  
  194. /****************************************************************************************************************/
  195. /****                                                                                                         ****/
  196. /**                                            safe_ShowMemory                                                       **/
  197. /****                                                                                                         ****/
  198. /****************************************************************************************************************/
  199.  
  200. void safe_ShowMemory(char* File,ULONG Line)
  201. {
  202.     struct MemNode *t;
  203.     BPTR con;
  204.  
  205.     if(memhead)
  206.     {
  207.         if (!(con = Open("CON:0/10/400/100/ShowMemory .../AUTO/CLOSE/WAIT/INACTIVE",MODE_NEWFILE)))
  208.         {
  209.             DisplayMsg("Can't open ShowMemory Window !!!");
  210.             return;
  211.         }
  212.  
  213.         t = memhead;
  214.         FPrintf(con, "%s %ld: Memory List\n", (long)File, Line);
  215.  
  216.         FPrintf(con,"    %-12s %-12s %-16s %-4s\n",(long)"Address", (long)"Size", (long)"File", (long)"Line");
  217.         FPrintf(con,"    %-12s %-12s %-16s %-4s\n",(long)"-------", (long)"----", (long)"----", (long)"----");
  218.  
  219.         while(t)
  220.         {
  221.             FPrintf(con,"    $%-11lx %-12ld %-16s %-4ld\n",(long)t,t->size,(long)t->File,t->Line);
  222.             t = t->child;
  223.         }
  224.     }
  225. }
  226.  
  227. /****************************************************************************************************************/
  228. /****                                                                                                         ****/
  229. /**                                            safe_ClearMemory                                                   **/
  230. /****                                                                                                         ****/
  231. /****************************************************************************************************************/
  232.  
  233. void safe_ClearMemory(BOOL Quiet,char* File,ULONG Line)
  234. {
  235.     static char msg[210];
  236.     struct MemNode *t1, *t2;
  237.     #ifdef DEBUG
  238.     static char ram[100];
  239.     #endif
  240.  
  241.     #ifdef DEBUG
  242.     sprintf(ram,"Total Ram used : %ld\nTotal RAM max used : %ld",Total_RAM,Max_RAM);
  243.     DisplayMsg(ram);
  244.     #endif
  245.  
  246.     if(memhead)
  247.     {
  248.         t1 = memhead;
  249.         if (!Quiet)
  250.         {
  251.             safe_ShowMemory(File,Line);
  252.             sprintf(msg, "Oups !! there is a BUG (a memory allocation problem)\nPlease report this bug to GenCode's Authors\nwith the next parameters (in ShowMemory Window)\n%s %ld: Freeing Memory List", File, Line);
  253.             DisplayMsg(msg);
  254.         }
  255.  
  256.         while(t1)
  257.         {
  258.             t2 = t1->child;
  259.             FreeMem(t1,t1->size+sizeof(struct MemNode));
  260.             t1 = t2;
  261.         }
  262.     }
  263.  
  264.     memhead = NULL;
  265. }
  266.  
  267. /****************************************************************************************************************/
  268. /****                                                                                                         ****/
  269. /**                                            OpenFile                                                           **/
  270. /****                                                                                                         ****/
  271. /****************************************************************************************************************/
  272.  
  273. BPTR safe_OpenFile( char *filename, LONG mode, BOOL quit, char *File, ULONG Line)
  274. {
  275.     struct    FileNode *t;
  276.     BPTR    file;
  277.  
  278.     if (filehead)
  279.         t = filehead;
  280.     else
  281.         t = NULL;
  282.  
  283.     if (!(file = Open(filename,mode)))
  284.     {
  285.         if (quit)
  286.         {
  287.             char    *msg;
  288.         
  289.             msg = AllocMemory(12+strlen(filename)+1,TRUE);
  290.             sprintf(msg,"Can't open %s\n",filename);
  291.             DisplayMsg(msg);
  292.             FreeMemory(msg);
  293.             safe_Quit();
  294.         }
  295.         else
  296.             return NULL;
  297.     }
  298.  
  299.     if (!(filehead = AllocMemory(sizeof(struct FileNode),FALSE)))
  300.     {
  301.         filehead = t;
  302.         Close(file);
  303.  
  304.         if (quit)
  305.             safe_Quit();
  306.         else
  307.             return NULL;
  308.     }
  309.     
  310.     filehead->child = t;
  311.     filehead->File = File;
  312.     filehead->Line = Line;
  313.     filehead->amigafile = TRUE;
  314.     if (!(filehead->filename = AllocMemory(strlen(filename)+1,FALSE)))
  315.     {
  316.         FreeMemory(filehead);
  317.         filehead = t;        
  318.         Close(file);
  319.  
  320.         if (quit)
  321.             safe_Quit();
  322.         else
  323.             return NULL;
  324.     }
  325.     strcpy(filehead->filename,filename);
  326.     filehead->file_desc.afile = file;
  327.  
  328.     return file;
  329. }
  330.  
  331. /****************************************************************************************************************/
  332. /****                                                                                                         ****/
  333. /**                                            CloseFile                                                           **/
  334. /****                                                                                                         ****/
  335. /****************************************************************************************************************/
  336.  
  337. BOOL safe_CloseFile(BPTR file, char *File, ULONG Line)
  338. {
  339.     static char            msg[150];
  340.     struct FileNode    *t1,*t2;
  341.  
  342.     if (file == NULL)
  343.         return TRUE;
  344.  
  345.     t1 = filehead;
  346.     t2 = NULL;
  347.  
  348.     while(t1)
  349.     {
  350.         if (file == t1->file_desc.afile)
  351.         {
  352.             if(t2)
  353.                 t2->child = t1->child; /* Remove block from the list */
  354.             else
  355.                 filehead = t1->child;
  356.                 
  357.             if (!Close(file))
  358.             {
  359.                 char    *msg;
  360.         
  361.                 if (!(msg=AllocMemory(13+strlen(t1->filename)+1,FALSE)))
  362.                 {
  363.                     FreeMemory(t1->filename);
  364.                     FreeMemory(t1);
  365.                     return FALSE;
  366.                 }
  367.  
  368.                 sprintf(msg,"Can't close %s\n",t1->filename);
  369.                 DisplayMsg(msg);
  370.                 FreeMemory(msg);
  371.                 FreeMemory(t1->filename);
  372.                 FreeMemory(t1);
  373.                 return FALSE;
  374.             }
  375.  
  376.             FreeMemory(t1->filename);
  377.             FreeMemory(t1);
  378.             return TRUE;
  379.         }
  380.  
  381.         t2 = t1;
  382.         t1 = t1->child;
  383.       }
  384.  
  385.     sprintf(msg,"The file that you want to close\nin file %s line %ld\nwas not opened or is already closed",File,Line);
  386.     DisplayMsg(msg);
  387.     
  388.     return FALSE;
  389. }
  390.  
  391. /****************************************************************************************************************/
  392. /****                                                                                                         ****/
  393. /**                                            fopenFile                                                           **/
  394. /****                                                                                                         ****/
  395. /****************************************************************************************************************/
  396.  
  397. FILE *safe_fopenFile(char *filename, char *mode, BOOL quit, char *File, ULONG Line)
  398. {
  399.     struct    FileNode *t;
  400.     FILE    *file;
  401.  
  402.     if (filehead)
  403.         t = filehead;
  404.     else
  405.         t = NULL;
  406.  
  407.     if (!(file = fopen(filename,mode)))
  408.     {
  409.         if (quit)
  410.         {
  411.             char    *msg;
  412.         
  413.             msg = AllocMemory(12+strlen(filename)+1,TRUE);
  414.             sprintf(msg,"Can't open %s\n",filename);
  415.             DisplayMsg(msg);
  416.             FreeMemory(msg);
  417.             safe_Quit();
  418.         }
  419.         else
  420.             return NULL;
  421.     }
  422.  
  423.     if (!(filehead = AllocMemory(sizeof(struct FileNode),FALSE)))
  424.     {
  425.         filehead = t;
  426.         fclose(file);
  427.  
  428.         if (quit)
  429.             safe_Quit();
  430.         else
  431.             return NULL;
  432.     }
  433.     
  434.     filehead->child = t;
  435.     filehead->File = File;
  436.     filehead->Line = Line;
  437.     filehead->amigafile = TRUE;
  438.     if (!(filehead->filename = AllocMemory(strlen(filename)+1,FALSE)))
  439.     {
  440.         FreeMemory(filehead);
  441.         filehead = t;        
  442.         fclose(file);
  443.  
  444.         if (quit)
  445.             safe_Quit();
  446.         else
  447.             return NULL;
  448.     }    
  449.     strcpy(filehead->filename,filename);
  450.     filehead->file_desc.cfile = file;
  451.  
  452.     return file;
  453. }
  454.  
  455. /****************************************************************************************************************/
  456. /****                                                                                                         ****/
  457. /**                                            fcloseFile                                                           **/
  458. /****                                                                                                         ****/
  459. /****************************************************************************************************************/
  460.  
  461. int safe_fcloseFile(FILE *file, char *File, ULONG Line)
  462. {
  463.     static char            msg[150];
  464.     struct FileNode    *t1,*t2;
  465.  
  466.     if (file == NULL)
  467.         return 0;
  468.  
  469.     t1 = filehead;
  470.     t2 = NULL;
  471.  
  472.     while(t1)
  473.     {
  474.         if (file == t1->file_desc.cfile)
  475.         {
  476.             int    ret;
  477.             
  478.             if(t2)
  479.                 t2->child = t1->child; /* Remove block from the list */
  480.             else
  481.                 filehead = t1->child;
  482.  
  483.             if (ret = fclose(file))
  484.             {
  485.                 char    *msg;
  486.  
  487.                 if (!(msg = AllocMemory(13+strlen(t1->filename)+1,FALSE)))
  488.                 {
  489.                     FreeMemory(t1->filename);
  490.                     FreeMemory(t1);
  491.                     return ret;
  492.                 }
  493.  
  494.                 sprintf(msg,"Can't close %s\n",file);
  495.                 DisplayMsg(msg);
  496.                 FreeMemory(msg);
  497.                 FreeMemory(t1->filename);
  498.                 FreeMemory(t1);
  499.                 return ret;
  500.             }
  501.             FreeMemory(t1->filename);
  502.             FreeMemory(t1);
  503.             return 0;
  504.         }
  505.  
  506.         t2 = t1;
  507.         t1 = t1->child;
  508.       }
  509.  
  510.     sprintf(msg,"The file that you want to close\nin file %s line %ld\nwas not opened or is already closed",File,Line);
  511.     DisplayMsg(msg);
  512.     
  513.     return 1;
  514. }
  515.  
  516. /****************************************************************************************************************/
  517. /****                                                                                                         ****/
  518. /**                                            ShowAllFiles                                                       **/
  519. /****                                                                                                         ****/
  520. /****************************************************************************************************************/
  521.  
  522. void safe_ShowAllFiles(char *File, ULONG Line)
  523. {
  524.     struct FileNode *t;
  525.     BPTR con;
  526.  
  527.     if(filehead)
  528.     {
  529.         if (!(con = Open("CON:0/10/400/100/ShowAllFiles .../AUTO/CLOSE/WAIT/INACTIVE",MODE_NEWFILE)))
  530.         {
  531.             DisplayMsg("Can't open ShowAllFiles Window !!!");
  532.             return;
  533.         }
  534.  
  535.         t = filehead;
  536.         FPrintf(con, "%s %ld: Files List\n", (long)File, Line);
  537.  
  538.         FPrintf(con,"    %-16s %-4s %-16s\n", (long)"File", (long)"Line", (long)"Filename");
  539.         FPrintf(con,"    %-16s %-4s %-16s\n", (long)"----", (long)"----", (long)"--------");
  540.  
  541.         while(t)
  542.         {
  543.             FPrintf(con,"    %-16s %-4ld %-16s\n",(long)t->File,t->Line,(long)t->filename);
  544.             t = t->child;
  545.         }
  546.     }
  547. }
  548.  
  549. /****************************************************************************************************************/
  550. /****                                                                                                         ****/
  551. /**                                            CloseAllFiles                                                       **/
  552. /****                                                                                                         ****/
  553. /****************************************************************************************************************/
  554.  
  555. void safe_CloseAllFiles(BOOL Quiet,char *File, ULONG Line)
  556. {
  557.     static char msg[210];
  558.     struct FileNode *t1, *t2;
  559.  
  560.     if(filehead)
  561.     {
  562.         t1 = filehead;
  563.         if (!Quiet)
  564.         {
  565.             sprintf(msg, "Oups !! there is a BUG (a file problem)\nPlease report this bug to GenCode's Authors\nwith the next parameters (in ShowAllFiles Window)\n%s %ld: Closing Files List", File, Line);
  566.             DisplayMsg(msg);
  567.             safe_ShowAllFiles(File,Line);
  568.         }
  569.  
  570.         while(t1)
  571.         {
  572.             t2 = t1->child;
  573.             (t1->amigafile) ? Close(t1->file_desc.afile) : fclose(t1->file_desc.cfile);
  574.             FreeMemory(t1->filename);
  575.             FreeMemory(t1);
  576.             t1 = t2;
  577.         }
  578.     }
  579.  
  580.     filehead = NULL;
  581. }
  582.  
  583. /****************************************************************************************************************/
  584. /****                                                                                                         ****/
  585. /**                                            CopyBlock                                                           **/
  586. /****                                                                                                         ****/
  587. /****************************************************************************************************************/
  588.  
  589. char *CopyBlock(FILE *file,char *Filechar,char *String,
  590.                 char *begin,char *end,
  591.                 char *MsgErrorBegin,char *MsgErrorEnd,
  592.                 char *MainFile)
  593. {
  594.     char ctmp;
  595.     char *str,*str1;
  596.  
  597.     if (!(str = strstr(String,begin)))
  598.     {
  599.         char *msg;
  600.  
  601.         msg = (char *)AllocMemory(80+strlen(MsgErrorBegin)+strlen(MainFile),TRUE);
  602.         sprintf(msg,"Can't find the next line in old file %s !!! :\n \"%s\"",MainFile,MsgErrorBegin);
  603.         DisplayMsg(msg);
  604.         FreeMemory(msg);
  605.         fcloseFile(file);
  606.         FreeMemory(Filechar);
  607.         safe_Quit();
  608.     }
  609.     if (end)
  610.     {
  611.         if (!(str1 = strstr(str,end)))
  612.         {
  613.             char *msg;
  614.  
  615.             msg = (char *)AllocMemory(80+strlen(MsgErrorEnd)+strlen(MainFile),TRUE);
  616.             sprintf(msg,"Can't find the next line in old file %s !!! :\n \"%s\"",MainFile,MsgErrorEnd);
  617.             DisplayMsg(msg);
  618.             FreeMemory(msg);
  619.             fcloseFile(file);
  620.             FreeMemory(Filechar);
  621.             safe_Quit();
  622.         }
  623.         ctmp=*str1;
  624.         *str1='\0';
  625.         fprintf(file,"%s",str+strlen(begin));
  626.         *str1=ctmp;
  627.         return str1;
  628.     }
  629.     else
  630.     {
  631.         fprintf(file,"%s",str+strlen(begin));
  632.         return NULL;
  633.     }
  634. }
  635.  
  636. /****************************************************************************************************************/
  637. /****                                                                                                         ****/
  638. /**                                            Indent                                                               **/
  639. /****                                                                                                         ****/
  640. /****************************************************************************************************************/
  641.  
  642. void Indent(FILE *file,int nb)
  643. {
  644.     int     i;
  645.  
  646.     for(i=0;i<nb;i++)
  647.         fprintf(file, "\t");
  648. }
  649.  
  650. /****************************************************************************************************************/
  651. /****                                                                                                         ****/
  652. /**                                            extract_dir                                                           **/
  653. /****                                                                                                         ****/
  654. /****************************************************************************************************************/
  655.  
  656. void extract_dir( char *filename )
  657. {
  658.     if (*filename)
  659.     {
  660.         filename=PathPart(filename);
  661.         *filename='\0';
  662.     }
  663. }
  664.  
  665. /****************************************************************************************************************/
  666. /****                                                                                                         ****/
  667. /**                                            extract_file                                                       **/
  668. /****                                                                                                         ****/
  669. /****************************************************************************************************************/
  670.  
  671. void extract_file( char * path, char * filename )
  672. {
  673.     strcpy(filename,FilePart(path));
  674. }
  675.  
  676. /****************************************************************************************************************/
  677. /****                                                                                                         ****/
  678. /**                                            add_extend                                                           **/
  679. /****                                                                                                         ****/
  680. /****************************************************************************************************************/
  681.  
  682. void add_extend( char *filename, char * extend )
  683. {
  684.     strcat(filename,extend);
  685. }
  686.  
  687. /****************************************************************************************************************/
  688. /****                                                                                                         ****/
  689. /**                                            remove_extend                                                       **/
  690. /****                                                                                                         ****/
  691. /****************************************************************************************************************/
  692.  
  693. void remove_extend( char *filename )
  694. {
  695.     char *aux;
  696.  
  697.     aux = strrchr(FilePart(filename),'.');
  698.     if (aux) *aux='\0';
  699. }
  700.  
  701. /****************************************************************************************************************/
  702. /****                                                                                                         ****/
  703. /**                                            change_extend                                                       **/
  704. /****                                                                                                         ****/
  705. /****************************************************************************************************************/
  706.  
  707. void change_extend( char *filename, char * extend )
  708. {
  709.     remove_extend(filename);
  710.     add_extend(filename,extend);
  711. }
  712.  
  713. /****************************************************************************************************************/
  714. /****                                                                                                         ****/
  715. /**                                            LoadFileInRam                                                       **/
  716. /****                                                                                                         ****/
  717. /****************************************************************************************************************/
  718.  
  719. /* Read a file and load it in memory */
  720. char * LoadFileInRAM(char *file,BOOL quit)
  721. {
  722.     BPTR                    TMPfile;
  723.     struct FileInfoBlock    *Info;
  724.     char                    *adr_file = NULL;
  725.     int                        size;
  726.  
  727.     if (TMPfile = OpenFile(file, MODE_OLDFILE, quit))
  728.     {
  729.         Info = AllocMemory(sizeof(struct FileInfoBlock),TRUE);
  730.         ExamineFH(TMPfile, Info);
  731.         size = Info->fib_Size; 
  732.         FreeMemory(Info);
  733.         if (!(adr_file = AllocMemory(size+1,quit)))
  734.         {
  735.             CloseFile(TMPfile);
  736.             return NULL;
  737.         }
  738.         FRead( TMPfile, adr_file, size, 1);
  739.         adr_file[size] = '\0';
  740.         CloseFile(TMPfile);
  741.         return adr_file;
  742.     }
  743.     else
  744.     {
  745.         return NULL;
  746.     }
  747. }
  748.  
  749. /****************************************************************************************************************/
  750. /****                                                                                                         ****/
  751. /**                                            GetCurrentDirectory                                                   **/
  752. /****                                                                                                         ****/
  753. /****************************************************************************************************************/
  754.  
  755. char *GetCurrentDirectory(void)
  756. {
  757.     BPTR     lock;
  758.     UWORD     len                 = 512;
  759.     char     *CurrentDirectory     = NULL;
  760.  
  761.     if (!(lock = Lock("PROGDIR:",ACCESS_READ)))
  762.     {
  763.         DisplayMsg("ERROR !!!!! CAN'T OBTAIN A LOCK TO THE CURRENT DIRECTORY !!!\n");
  764.         safe_Quit();
  765.     }
  766.     do
  767.     {
  768.         if (CurrentDirectory)
  769.             FreeMemory(CurrentDirectory);
  770.         if (!(CurrentDirectory = AllocMemory(len+1,FALSE)))
  771.         {
  772.             UnLock(lock);
  773.             safe_Quit();
  774.         }
  775.         NameFromLock(lock,CurrentDirectory,len);
  776.         len*=2;
  777.     }while(IoErr()==ERROR_LINE_TOO_LONG);
  778.     UnLock(lock);
  779.     return CurrentDirectory;
  780. }
  781.  
  782. /****************************************************************************************************************/
  783. /****                                                                                                         ****/
  784. /**                                            CopyFile                                                           **/
  785. /****                                                                                                         ****/
  786. /****************************************************************************************************************/
  787. BOOL CopyFile(char *FromFile,char *ToFile)
  788. {
  789.     char                    *buff;
  790.     char                    *msg;
  791.     BPTR                    File;
  792.     struct FileInfoBlock    *Info;
  793.     int                        size;
  794.  
  795.     if (!(File = OpenFile(FromFile,MODE_OLDFILE, FALSE)))
  796.     {
  797.         return FALSE;
  798.     }
  799.     Info = AllocMemory(sizeof(struct FileInfoBlock),TRUE);
  800.     ExamineFH(File, Info);
  801.     size = Info->fib_Size; 
  802.     FreeMemory(Info);
  803.     if (!(buff = AllocMemory(size,FALSE)))
  804.     {
  805.         CloseFile(File);
  806.         return FALSE;
  807.     }
  808.     FRead(File,buff,size,1);
  809.     CloseFile(File);
  810.  
  811.     if (!(File = OpenFile(ToFile,MODE_NEWFILE,FALSE)))
  812.     {
  813.         if (!(msg = AllocMemory(strlen(ToFile)+1+13,FALSE)))
  814.         {
  815.             return FALSE;
  816.         }
  817.         sprintf(msg,"Can't write \"%s\"",ToFile);
  818.         DisplayMsg(msg);
  819.         FreeMemory(msg);
  820.         return FALSE;
  821.     }
  822.     FWrite(File,buff,size,1);
  823.     FreeMemory(buff);
  824.     CloseFile(File);
  825.     return TRUE;
  826. }
  827.